$(document).ready(function () { 
	
	$('.form_date1').datetimepicker({
		weekStart: 1,
		todayBtn: 1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		minView: 2,
		forceParse: 0,
		clearBtn:true
	});

	var mzjson = ajaxResult("get",false,"../../mbxtglbasetableservlet.getPullData.do",{tn:"mb_gy_zq"});
	loadSelect("select[name=mz]",mzjson.rows,'id','name');
	
	var gjjson = ajaxResult("get",false,"../../mbxtglbasetableservlet.getPullData.do",{tn:"mb_gy_gj"});
	loadSelect("select[name=gj]",gjjson.rows,'id','name');
	
	var gjjson = ajaxResult("get",false,"../../mbxtglbasetableservlet.getPullData.do",{tn:"mb_gy_zjlx"});
	loadSelect("select[name=zjlx]",gjjson.rows,'id','name');
	
	// var jgjson = ajaxResult("get",false,"../../mbxtgljgservlet.getDataTwoJg.do");	
	// loadSelect("select[name=szjg]",jgjson.data,'zdbh','jgmc');

	createTable('#userInfo','../../mbxtglzjservlet.getData.do','zdbh',tblColTitle,null,null);

	$("#resetBtnT").click(function(){
		$("#role_select").empty();
		oliIdArray = [];
		$("li").removeAttr("class");
	//	$(".selectUl").hide();
	})

	
	$("#resetBtnTupdata").click(function(){
		$("#role_selectupdata").empty();
		oliIdArrayupdata = [];
		$("li").removeAttr("class");
	//	$(".selectUl").hide();
	})
	
	
	$("#addTcBtn").click(function(){
		var fromParams=$("#form_Tc").serializeObject();
		var mzjson = ajaxResult("get",false,"../../mbxtglzjservlet.addYjtc.do",{name:fromParams.yjtc});
		if(mzjson.status=="OK"){
			alertFunc("添加成功");
			$("#addTC").hide();
			getTc();
		}else{
			alertFunc("添加失败");
			$("#addTC").hide();
			getTc();
		}
	})
	$("#resetBtn").click(function(){
		updataSelectOrInput('yjtc','yjtcp','resetBtn');
	})
		$("#resetBtn1").click(function(){
		updataSelectOrInput('yjtcS','yjtcI','resetBtn1');
	})
	
	$("#query_btn").click(function(){
		refreshTable("#userInfo","#form_bd");
	});
	$(".newAdd").click(function(){
		var mzjson = ajaxResult("get",false,"../../mbxtglbasetableservlet.getPullData.do",{tn:"mb_gy_zq"});
		loadSelect("select[name=mz]",mzjson.rows,'id','name');
	});
	$("#baocun").click(function(){
		  if(!checkform("#form_bds")){
			  return;
		  }
		 
		var params =$("#form_bds").serializeObject();	
		 if(params.dh!=""){
			if(!checkphone($("#form_bds #dh"))){
				return;
			}
		  }
		  if(params.dzyj!=""){
			  if(!checkemail($("#form_bds #dzyj"))){
				  return;
			  }
		  }
		 
		params.yjtc = $("#role_select").attr("oliid");
		var addJson = ajaxResult("get",false,"../../mbxtglzjservlet.insert.do",params);
		if(addJson.status=="OK"){
			alertFunc("添加成功")
			
		}else{
			alertFunc("添加失败")
		}
		$('#form_bds')[0].reset();
		$("#role_select").empty();
		oliIdArray = [];
		$("li").removeAttr("class");
		refreshTable("#userInfo","#form_bd");
		$('#exampleModal').modal('hide');
	});
	
	
	
	$("#xiugai").click(function(){
		  if(!checkform("#form_bd1")){
			  return;
		  }
		 
		var params =$("#form_bd1").serializeObject();	
		 if(params.dh!=""){
			if(!checkphone($("#form_bd1 #dh"))){
				return;
			}
		  }
		  if(params.dzyj!=""){
			  if(!checkemail($("#form_bd1 #dzyj"))){
				  return;
			  }
		  }
			params.yjtc = $("#role_selectupdata").attr("oliid");
		var addJson = ajaxResult("get",false,"../../mbxtglzjservlet.update.do",params);
		if(addJson.status=="OK"){
			alertFunc("修改成功");
		}else{
			alertFunc("修改失败");
		}
		refreshTableAtCurPage("#userInfo","#form_bd");
		$('#exampleModal1').modal('hide');
	});
	
	
});
function refreshTable(tableid,formid){	 
	 var params =$(formid).serializeObject();		  
	 params["pageNumber"] = 1;
	 params["limit"] = pageSize;
	 params["offset"] = 0;	 
	 $(tableid).bootstrapTable('refresh',{query:params});	
};


/*function updataSelectOrInput(select,input,btn){
			var docName = $("#"+btn).attr("value");
			if(docName=="编辑"){
				$("#"+select).css("display","none");
				$("#"+select).removeAttr("class");
				$("#"+input).css("display","block");
				$("#"+input).attr("class","form-control");
				$("#"+btn).attr("value","选择");
			}else{
				$("#"+select).css("display","block");
				$("#"+input).removeAttr("class");
				$("#"+input).css("display","none");
				$("#"+select).attr("class","form-control");
				$("#"+btn).attr("value","编辑");
			}
}*/

function selectInfo(zdbh){
	getTcinfo();
	var mzjson = ajaxResult("get",false,"../../mbxtglbasetableservlet.getPullData.do",{tn:"mb_gy_zq"});
	loadSelect("select[name=mz]",mzjson.rows,'id','name');
	 var json = ajaxResult("get",false,"../../mbxtglzjservlet.getDataById.do",{zdbh:zdbh});
	loadData("#form_bd2",json.rows);
	var tcStr = json.rows.yjtc;
	var Uarry=$(".selectUlinfo li");
	if(tcStr.indexOf(",") >= 0 ) { 
		$("#role_selectinfo").empty();
		oliIdArrayupdata = [];
		$("li").removeAttr("class");
		var tcList = tcStr.split(",");
		for(i = 0; i < Uarry.length; i++){
			for(j=0;j<tcList.length;j++){
				if(Uarry[i].attributes.oliid.nodeValue==tcList[j]){
					$(".selectUlinfo li[oliid="+tcList[j]+"]").attr("class","actived_li");
					oliIdArray.push(tcList[j]);
					$("#role_selectinfo").attr("oliId",oliIdArray);
					$("#role_selectinfo").append("<span class='person_root'><span>"+Uarry[i].attributes.oliName.nodeValue+"</span></span>");
				}
			}
		  }
	}else{
		$("#role_selectinfo").empty();
		oliIdArrayupdata = [];
		$("li").removeAttr("class");
		$(".selectUlinfo li[oliid="+tcStr+"]").attr("class","actived_li");
		oliIdArray.push(tcStr);
		$("#role_selectinfo").attr("oliId",oliIdArray);
		for(i = 0; i < Uarry.length; i++){
			if(Uarry[i].attributes.oliid.nodeValue==tcStr){
				$("#role_selectinfo").append("<span class='person_root'><span>"+Uarry[i].attributes.oliName.nodeValue+"</span></span>");
			}	
		}
	}	
}

function loadDataInFrom(formID,jsonStr){
	var obj =jsonStr;
	var key,value,tagName,type,arr;
	for(x in obj){
		key = x;
		value = obj[x];		
		
		if(key=="yhmm"){
			
		}else{
		//$(formID+" [name='"+key+"'],"+formID+" [name='"+key+"[]']").each(function(){
		$(formID+" [name='"+key+"'],"+formID+" [name='"+key+"[]']").each(function(){
			tagName = $(this)[0].tagName;
			type = $(this).attr('type');
			if(tagName=='INPUT'){
				if(type=='radio'){
					$(this).prop('checked',$(this).val()==value);
				}else if(type=='checkbox'){
					if((value+"").indexOf(',')>-1){
						arr = value.split(',');
					}else{
						arr = value;
					}
					for(var i =0;i<arr.length;i++){
						if($(this).val()==arr[i]){
							$(this).prop('checked',true);
							break;
						}
					}
				}else{
					$(this).val(value);
				}
			}else if(tagName=='SELECT' || tagName=='TEXTAREA'){
				$(this).val(value);
			}else if(tagName=='LABEL'){
				$(this).text(value);
			}else{
				$(this).val(value);
			}			
		});
		
		}
	}
	
	
}
function selectInfoTOupdate(zdbh){
	getTc1();
	var mzjson = ajaxResult("get",false,"../../mbxtglbasetableservlet.getPullData.do",{tn:"mb_gy_zq"});
	loadSelect("select[name=mz]",mzjson.rows,'id','name');
	 var json = ajaxResult("get",false,"../../mbxtglzjservlet.getDataById.do",{zdbh:zdbh});
	 loadDataInFrom("#form_bd1",json.rows);
//	var tcStr = JSON.stringify(json.rows.yjtc);
	var tcStr = json.rows.yjtc;
	$("#role_selectupdata").removeAttr("oliid");
	$("#role_selectupdata").attr("oliId",tcStr);
	var Uarry=$(".selectUlupdata li");
	if(tcStr.indexOf(",") >= 0 ) { 
		$("#role_selectupdata").empty();
		oliIdArrayupdata = [];
		$("li").removeAttr("class");
		var tcList = tcStr.split(",");
		for(i = 0; i < Uarry.length; i++){
			for(j=0;j<tcList.length;j++){
					if(Uarry[i].attributes.oliid.nodeValue==tcList[j]){
						$("#updataSelt li[oliid="+tcList[j]+"]").attr("class","actived_li");
						//oliIdArrayupdata.push(tcList[j]);
						
						$("#role_selectupdata").append("<span class='person_root'><span>"+Uarry[i].attributes.oliName.nodeValue+"</span><i class='close' oliId='" + tcList[j] + "'  onclick='oliDeleteupdata(this)'>x</i></span>");
					}
			}
		  }
	
	
		
		
		
	}else{
		$("#role_selectupdata").empty();
		oliIdArrayupdata = [];
		$("li").removeAttr("class");
		$("#updataSelt li[oliid="+tcStr+"]").attr("class","actived_li");
		//oliIdArray.push(tcStr);
	//	$("#role_selectupdata").attr("oliId",oliIdArray);
		for(i = 0; i < Uarry.length; i++){
			if(Uarry[i].attributes.oliid.nodeValue==tcStr){
				$("#role_selectupdata").append("<span class='person_root'><span>"+Uarry[i].attributes.oliName.nodeValue+"</span><i class='close' oliId='" + tcStr + "' >x</i></span>");
			}	
		}
		}

	

}



// function tableJs(){
// 	var a= $("#userInfo").bootstrapTable('getSelections');

// 	if(a.length==0){
// 		 $(".AddJs").removeAttr("data-toggle");
// 		 $(".AddJs").removeAttr("data-target");
// 		alertFunc("请选择要分配的专家");
// 		return false;
// 	}
	  
// 	 $(".AddJs").attr("data-toggle","modal");
// 	 $(".AddJs").attr("data-target","#exampleModal6");
// 	createTable('#userdb','../../mbxtglqxservlet.getJsList.do','zdbh',us,null,null);
// }
var tblColTitle = [ 
	{ title: '序号', width: 10, align: "center", formatter: function (value, row, index) { return index + 1; } },
	{ title: '专家姓名', field: 'xm' },
	{ title: '证件类型', field: 'zjlxName' },
	{ title: '证件号码', field: 'zjhm' },
	{ title: '性别', field: 'xb' ,width: 10,formatter: function (value, row, index) { 
		var result="";
		if(row.xb==1){
			result="男";
		}else{
			result="女";
		}
		return result;
	}},
	{ title: '国籍', field: 'gjName' },
	{ title: '民族', field: 'zqName' },
	{ title: '电话', field: 'dh' },
	{ title: '电子邮件', field: 'dzyj' },
	{ title: '操作', field: '', formatter: function (value, row, index) {
		 var result="";
		 result = "<a href='#' onclick='selectInfo("+row.zdbh+")' data-toggle='modal' data-target='#exampleModal2'>查看</a>&nbsp;&nbsp;&nbsp;"+
		 "<a href='#' onclick='selectInfoTOupdate("+row.zdbh+")' data-toggle='modal' data-target='#exampleModal1'>修改</a>&nbsp;&nbsp;&nbsp;"+
		 "<a href='#' onclick='tableJs("+row.zdbh+")' data-toggle='modal' data-target='#exampleModal6'>授权</a>&nbsp;&nbsp;&nbsp;" +
		 "<a href='#' onclick='selectJs("+row.zdbh+")' data-toggle='modal' data-target='#exampleModal5'>查看权限</a>&nbsp;&nbsp;&nbsp;" +
		 "<a href='#' onclick='delZj("+row.zdbh+")'>注销</a>&nbsp;&nbsp;&nbsp;";
		 return result;
	}}
];
var userID;
function tableJs(zdbh){
	userID=zdbh;
	$("#userdb").bootstrapTable('destroy');
	createTable('#userdb','../../mbxtglqxservlet.getJsListByPerson.do','zdbh',us,{"zdbh":zdbh,"sfwzj":1},null);
}
function selectJs(zdbh){
	$("#jsdb").bootstrapTable('destroy');
	queryTableNoPage('#jsdb','../../mbxtglzjservlet.getjs.do',jsColTitle,{userid:zdbh});
}
function delZj(zdbh){
	window.top.bootbox.setLocale("zh_CN");
	window.top.bootbox.confirm("确认删除该专家吗？",function(r){
		if(r){
			var deljson = ajaxResult("get",false,"../../mbxtglzjservlet.delete.do",{zdbh:zdbh});
			if(deljson.status=="OK"){
				alertFunc("删除成功");
			}else{
				alertFunc("删除失败");
			}
			refreshTableAtCurPage("#userInfo","#form_bd");
		}
	});
}
function fenpei(zdbh){
	var a= $("#userInfo").bootstrapTable('getSelections');
	var json = ajaxResult("get",false,"../../mbxtglzjservlet.assignJs.do",{userid:userID,roleid:zdbh});
	if(json.status=="OK"){
		//alertFunc("分配成功");
		//$('#exampleModal6').modal('hide');
	}else{
		//alertFunc("分配失败");
		//$('#exampleModal6').modal('hide');
	}
	userID="";
	//refreshTableAtCurPage("#userInfo","#form_bd");
}
var us = [
	{ title: '名称', field: 'jsmc' },
	{ title: '专家类型', field: 'zjlx'},
	{ title: '描述', field: 'jsms' ,
 	   formatter: function(value, row, index) {
	        	var result="";	 console.log(row.jsms);
	        	if(row.jsms!=null && row.jsms!=""){
	        	if(row.jsms.length>27){
	        		result ="<span title='"+row.jsms+"'>"+row.jsms.substring(0,27)+"...</span>";  	       	        		
	        	}else{
	        		result =row.jsms;
	        	}
	        	}
       	return result;
		  } },
	{ title: '授权', field: '', formatter: function (value, row, index) {
		 var result="";
		 var ischeck = row.rybm;
		 if(ischeck!=null&&ischeck!=""){
			result="<input type='radio' checked=true name='radio-"+row.zdbh+"' onclick='fenpei("+JSON.stringify(row.zdbh)+")'>";
		 }else{
			result="<input type='radio' name='radio-"+row.zdbh+"' onclick='fenpei("+JSON.stringify(row.zdbh)+")'>";
		 }
		 //result="<a href='#' onclick='fenpei("+JSON.stringify(row.jsbm)+")'>分配</a>&nbsp;&nbsp;&nbsp;";
		 return result;
	}}
];

var jsColTitle = [ 
	//{ title: '角色编码', field: 'jsbm' },
	//{ title: '角色状态', field: 'jszt' },
	{ title: '名称', field: 'jsmc' },
	{ title: '专家类型', field: 'zjlx'},
	{ title: '描述', field: 'jsms' }
];